﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Gnosis.Alexandria.Data;

namespace Gnosis.Alexandria.Views
{
    public partial class BrowseView : NavigateView
    {
        public BrowseView()
        {
            InitializeComponent();
            webBrowser.Navigated += new WebBrowserNavigatedEventHandler(webBrowser_Navigated);
        }

        private void Navigate(string url)
        {
            SetSearchText(url);
            webBrowser.Navigate(url);
        }

        private void webBrowser_Navigated(object sender, WebBrowserNavigatedEventArgs e)
        {
            SetSearchText(webBrowser.Url.ToString());
        }

        protected override void HandleMessage(IMessageOld message)
        {
            switch (message.Type)
            {
                case MessageTypes.RequestBrowseViewLoad:
                {
                    var data = message.Body as NavigateToUrlData;
                    Navigate(data.Url);
                    break;
                }
                case MessageTypes.RequestNavigateToUrl:
                {
                    var data = message.Body as NavigateToUrlData;
                    Navigate(data.Url);
                    break;
                }
                case MessageTypes.RequestNavigateViewSearch:
                {
                    var data = message.Body as SearchRequestData;
                    Navigate(data.Search);
                    break;
                }
                case MessageTypes.RequestNavigateViewBack:
                {
                    webBrowser.GoBack();
                    break;
                }
                case MessageTypes.RequestNavigateViewForward:
                {
                    webBrowser.GoForward();
                    break;
                }
                case MessageTypes.RequestNavigateViewRefresh:
                {
                    webBrowser.Refresh();
                    break;
                }
                default:
                    base.HandleMessage(message);
                    break;
            }
        }
    }
}
